package com.li.server.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionUtil {
	public static final ThreadLocal<Connection> conHolder = new ThreadLocal<Connection>();

	static {
		try {
//			Class.forName("com.mysql.jdbc.Driver");
			Class.forName("org.h2.Driver");
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static Connection getNewConnection() {
		Connection connectMySQL = null;
		try {
//			connectMySQL = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root" ,"abc" );
			connectMySQL = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test","sa" ,"" );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connectMySQL;
	}

	public static Connection getCurrentConnection() {
		Connection conn = conHolder.get();
		if (conn == null) {
			conn = getNewConnection();
			conHolder.set(conn);
		}
		return conn;
	}

	public static void releaseConnection() {
		Connection conn = conHolder.get();
		conHolder.set(null);
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

